package com.ruoyi.info.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.CmsCommentMapper;
import com.ruoyi.info.domain.CmsComment;
import com.ruoyi.info.service.ICmsCommentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 评论Service业务层处理
 * 
 * @author xwj
 * @date 2023-07-07
 */
@Service
public class CmsCommentServiceImpl implements ICmsCommentService {
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    @Autowired
    private CmsCommentMapper cmsCommentMapper;

    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public CmsComment selectCmsCommentById(Long id)
    {
        return cmsCommentMapper.selectCmsCommentById(id);
    }

    /**
     * 查询评论列表
     * 
     * @param cmsComment 评论
     * @return 评论
     */
    @Override
    public List<CmsComment> selectCmsCommentList(CmsComment cmsComment)
    {
        return cmsCommentMapper.selectCmsCommentList(cmsComment);
    }

    /**
     * 新增评论
     * 
     * @param cmsComment 评论
     * @return 结果
     */
    @Override
    public int insertCmsComment(CmsComment cmsComment)
    {
        cmsComment.setCreateTime(DateUtils.getNowDate());
        return cmsCommentMapper.insertCmsComment(cmsComment);
    }

    /**
     * 修改评论
     * 
     * @param cmsComment 评论
     * @return 结果
     */
    @Override
    public int updateCmsComment(CmsComment cmsComment)
    {
        cmsComment.setUpdateTime(DateUtils.getNowDate());
        return cmsCommentMapper.updateCmsComment(cmsComment);
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCmsCommentByIds(String ids)
    {
        return cmsCommentMapper.deleteCmsCommentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除评论信息
     * 
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteCmsCommentById(Long id)
    {
        return cmsCommentMapper.deleteCmsCommentById(id);
    }

    @Override
    public List<CmsComment> getContentComments(Long contentId) {
        List<CmsComment> list = cmsCommentMapper.getContentCOmments(contentId);

        List<CmsComment> trees = Lists.newArrayList();

        Date now = new Date();

        // parentId 为 null的表示第一层
        for (CmsComment cmsComment : list) {
            if (cmsComment.getParentId() == null) {
                trees.add(cmsComment);
            }

            cmsComment.setTimeText(buildTimeText(cmsComment.getCreateTime(), now));
        }
        CmsComment findRoot = null;
        for (CmsComment cmsComment : list) {
            findRoot = findRoot(cmsComment.getParentId(), list);

            if(findRoot != null) {
                if(findRoot.getChildren() == null) {
                    findRoot.setChildren(Lists.newArrayList());
                }
                findRoot.getChildren().add(cmsComment);
                final Long findRootId = findRoot.getId();

                boolean isExist = trees.stream().anyMatch(t -> t.getId().equals(findRootId));
                if(!isExist) {
                    trees.add(findRoot);
                }

            } else {
                final Long currId = cmsComment.getId();
                boolean isExist = trees.stream().anyMatch(t -> t.getId().equals(currId));
                if(!isExist) {
                    trees.add(cmsComment); // 一个没有顶级的节点，自己就是顶级
                }
            }
        }

        return trees;
    }

    @Override
    public int getCommentSize(Long contentId) {
        return cmsCommentMapper.getCommentSize(contentId);
    }

    private String buildTimeText(Date parse, Date now) {
        long diff = now.getTime() - parse.getTime();
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long minute = (diff / (60 * 1000) - day * 24 * 60 - hour * 60);
        long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
        if(day > 2) {
            return sd.format(parse);
        }  else if (day > 0) {
            return day + "天前";
        } else if(hour > 0) {
            return hour + "小时前";
        } else if(minute > 0) {
            return minute + "分钟前";
        } else if(second > 30) {
            return second + "秒前";
        } else if(second > 0) {
            return "刚刚";
        }

        return "";
    }

    private CmsComment findRoot(Long parentId, List<CmsComment> list) {
        for (CmsComment cmsComment : list) {
            if (cmsComment.getId().equals(parentId)) {
                if(cmsComment.getParentId() == null) {
                    return cmsComment;
                }
               return findRoot(cmsComment.getParentId(), list);
            }
        }
        return null;
    }
}
